// packageB/serviceDetail/index.js
const APP = getApp()
Page({
    imagePath: '',
    sharePath:"",

    isSave: false,
    /**
     * 页面的初始数据
     */
    data: {
        shopInfo: '',
        info: '',
        commit: "",
        image: "",
        resourceURL: wx.api.host + '/',
        fxshow: false,
        total: 0
    },
    //图片预览
    previewImages(obj) {
        let e = obj.currentTarget.dataset.e
        let arr = []
        for (let index = 0; index < e.length; index++) {
            let element = this.data.resourceURL + e[index].url;
            arr.push(element)
        }
        wx.previewImage({
            urls: arr,
            current: obj.currentTarget.dataset.curr
        })
    },

    saveImage() {

        if (this.imagePath && typeof this.imagePath === 'string') {
            this.isSave = false;
            wx.authorize({
                scope: 'scope.writePhotosAlbum',
            }).then(res => {
                console.log(this.imagePath);
                return wx.saveImageToPhotosAlbum({
                    filePath: this.imagePath,
                })
            }).then(res => {
                console.log(res);
                wx.showToast({
                    title: '保存成功',
                })
            }).catch(err => {
                console.error(err);
                wx.openSetting()
                wx.showToast({
                    title: '保存失败',
                })
            });
        }
    },
    // 分享提示
    tipShare() {
        wx.showLoading({
            title: '海报生成中',
        })
        let {
            info, //详情的解构
            shopInfo, //商铺的解构
            codePath, //太阳码
            image, //轮播图
        } = this.data

        this.setData({

            paintPallette: {
                width: '588rpx',
                height: '752rpx',
                background: '#FFFFFF',
                views: [
                   
                    {
                        type: "image",
                        url: `${wx.api.host}/${image[0].url}`,
                        css: {
                            width: "588rpx",
                            height: "470.4rpx",
                            mode: "aspectFill",
                            right: "0rpx",
                            top: "0rpx",
                            borderRadius: "0 0 106rpx  106rpx"
                        },
                    },
                     {
                        type: "image",
                        url: `/packageB/assets/img/bgBox.png`,
                        css: {
                            width: "588rpx",
                            height: "470.4rpx",
                            mode: "aspectFill",
                            right: "0rpx",
                            top: "0rpx",
                            borderRadius: "0 0 106rpx  106rpx",

                        },
                    },
                    {
                        type: "image",
                        url: codePath,
                        css: {
                            width: "168rpx",
                            height: "168rpx",
                            mode: "aspectFill",
                            right: "24rpx",
                            bottom: "243rpx",
                            borderRadius: "50%"
                        },
                    },
                    {
                        type: "text",
                        text: "￥",
                        css: {
                            left: "24rpx",
                            bottom: "234rpx",
                            fontSize: "24rpx",
                            color: "#F33224",
                            fontWeight: "bold"
                        },
                    },
                    {
                        type: "text",
                        text: info.sale_price,
                        css: {
                            left: "49rpx",
                            bottom: "234rpx",
                            fontSize: "40rpx",
                            color: "#F33224",
                            fontWeight: "bold"
                        },
                    },
                    {
                        type: "text",
                        text: info.service_name,
                        css: {
                            left: "24rpx",
                            bottom: "192rpx",
                            fontSize: "32rpx",
                            color: "#202327",
                            fontWeight: "bold"
                        },
                    },
                    {
                        id: 'one',
                        type: 'rect',
                        css: {
                            width: '540rpx',
                            height: '88rpx',
                            left: '24rpx',
                            bottom: '80rpx',
                            color: "#FF7733",
                            borderRadius: "44rpx"
                        },
                    },
                    {
                        type: "text",
                        text: "点击保存照片 扫码下单",
                        css: {
                            left: "172rpx",
                            bottom: "110rpx",
                            fontSize: "28rpx",
                            color: "#FFFFFF",
                        },
                    },
                    {
                        type: "image",
                        url: "../../static/wx.png",
                        css: {
                            width: "42rpx",
                            height: "34rpx",
                            mode: "aspectFill",
                            left: "124rpx",
                            bottom: "106rpx",
                        },
                    },
                    {
                        type: "image",
                        url: shopInfo.imagesurl,
                        css: {
                            width: "34rpx",
                            height: "34rpx",
                            mode: "aspectFill",
                            left: "24rpx",
                            bottom: "24rpx",

                        },
                    },
                    {
                        type: "text",
                        text: shopInfo.shop_name,
                        css: {
                            left: "70rpx",
                            bottom: "30rpx",
                            fontSize: "24rpx",
                            color: "#06021A",
                        },
                    },

                ],
            },

        })
        setTimeout(() => {
            wx.hideLoading()
            this.setData({
                fxshow: true,
            })
        }, 1000);

    },
    onImgOK(e) {
   
        this.imagePath = e.detail.path;
        this.setData({
            sharePath: this.imagePath,
        });
     
        if (this.isSave) {
            this.saveImage(this.imagePath);
        }
    },
    onCloseShare() {
        this.setData({
            fxshow: false
        });
    },


    //获取服务详情
    getDetail() {
        wx.http.post(wx.api.serviceinfo, {
            service_id: this.data.service_id
        }).then(res => {
            if (res.code == 1) {
                let image = res.data.image.length ? res.data.image : [{
                    url: res.data.service.top_pic
                }]
                this.setData({
                    info: res.data.service,
                    image,

                })

                this.shopinfo(res.data.service.shop_id)
                // this.getProductCommit()
            }
        })

    },
    // 获取店铺详情
    shopinfo(shop_id) {
        wx.http.get(wx.api.shopinfo, {
            shop_id
        }).then(res => {

            if (res.code == 1) {
                res.data.imagesurl = this.data.resourceURL + res.data.imagesurl
                this.setData({
                    shopInfo: res.data
                })
            }
        })
    },
    //获取评价
    getProductCommit() {
        wx.http.get(wx.api.getproductcommit, {
            product_id: this.data.service_id,
            type: 3,
            page: 1,
            limit: 1,
        }).then(res => {
            if (res.code == 1) {

                this.setData({
                    commit: res.data.data,
                    total: res.data.total,

                })

            }
        })
    },
    //跳转评论列表
    toCommonlist() {
        wx.navigateTo({
            url: '../comments/index?id=' + this.data.info.service_id + '&type=3'
        })
    },
    //跳转店铺
    toShop() {
        wx.navigateTo({
            url: '../serviceShop/index?shop_id=' + this.data.info.shop_id
        })
    },
    booking() {
        wx.setStorageSync('serviseDetail', this.data.info)
        wx.navigateTo({
            url: '../settlement/index'
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        let id = null
        if (options.scene) {
            id = APP.globalData.getQueryVariable(decodeURIComponent(options.scene)).service_id
        }
        this.setData({
            service_id: id || options.service_id,
        })
        this.getDetail()
        this.getProductCommit()

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        wx.http.post(wx.api.GetNormalQrCode, {
            url: `packageB/serviceDetail/index?service_id=${this.data.service_id}`
        }).then(res => {
            const path = `${wx.api.host}/${res.data.path}`
            console.log(path);
            this.setData({
                codePath: path
            })

        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})